﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class components_Payments : System.Web.UI.UserControl
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Request.QueryString["idPayment"] != null && !string.IsNullOrEmpty(Request.QueryString["idPayment"]))
        {
            read();
        }
        else
        {
            //TODO:PREPARE TO INSERT

        }
    }

    private void read()
    {
        int idPayment;
        if (!int.TryParse(Request.QueryString["idPayment"], out idPayment))
        {
            //TODO: manage error
        }
        
        using (DataClassesDataContext db = new DataClassesDataContext())
        {
            Payments payment = db.Payments.Single(p => p.idPayment == idPayment);
            if(payment.confirmed )
            {
                common.DisableControls(this);
                lblConfirmed.Text = "Pago confirmado";
            }
            else
            {
                lblConfirmed.Text = "Pago sin confirmar";
            }
            txtNumber.Text = payment.number;
            txtBank.Text = payment.bank;
            txtAmount.Text = payment.amount.ToString();
            lblName.Text = payment.Users.name;

               
        }
    }

    protected void admBtnConfirm_Click(object sender, EventArgs e)
    {
        int idPayment;
        if (!int.TryParse(Request.QueryString["idPayment"], out idPayment))
        {
            //TODO: manage error
        }

        using (DataClassesDataContext db = new DataClassesDataContext())
        {
            Payments payment = db.Payments.Single(p => p.idPayment == idPayment);
            if (payment.confirmed)
            {
               //todo: manage Error
            }
            else
            {
                payment.confirmed = true;
                payment.Users.credit += payment.amount;
                db.SubmitChanges();
                common.DisableControls(this);
                lblConfirmed.Text = "Pago confirmado";

            }
         


        }

    }
}